package wypozyczalnia.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

//import wypozyczalnia.domain.Car;
import wypozyczalnia.domain.Movie;
import wypozyczalnia.domain.Photo;
import wypozyczalnia.service.CarService;
import wypozyczalnia.service.MovieService;
import wypozyczalnia.service.PhotoService;

@Controller
@RequestMapping(value = "/offerView/*")
public class OfferViewController {

	// private final Logger logger =
	// Logger.getLogger(OfferViewController.class);

//	@Autowired
//	CarService carService;

	@Autowired
	PhotoService photoService;

	
	public static final int PAGINATE_NO=5;

	@RequestMapping(value = "view.do", params = "page")
	public String dispalyOffer(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			Model model, @RequestParam Long page) {
//		int all = carService.getAvailableCars().size();
//		int pages = (int) Math.ceil(((double) all / (double) PAGINATE_NO));
//		if (page < 0 || page == null || page > pages) {
//			page = new Long(1);
//		}
//		List<Car> list = carService.getAvalibleCarsByPage(page.intValue());
//		ArrayList<Car> cars = new ArrayList<Car>(list);

//		List<Integer> p = getPages(pages, page.intValue());
//		model.addAttribute("actual", page);
//		model.addAttribute("last", pages);
//		model.addAttribute("pages", p);
//		model.addAttribute("cars", cars);
		
//		List<String> categories = carService.getAvalibleBrands();
		
//		model.addAttribute("categories", categories);
		model.addAttribute("showCategories", true);
		
		return "offerView";
	}

	@RequestMapping(value = "view.do")
	public String dispalyOfferNoParam(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			Model model) {
//		List<Car> list = carService.getAvailableCars();
//		int all = list.size();
//		int pages = (int) Math.ceil(((double) all / (double) PAGINATE_NO));
//		List<Car> l = carService.getAvalibleCarsByPage(1);
//		ArrayList<Car> cars = new ArrayList<Car>(l);
//		System.out.println("Pages: " + pages);
//		List<Integer> p = getPages(pages, 1);
//		model.addAttribute("last", pages);
//		model.addAttribute("actual", 1);
//		model.addAttribute("pages", p);
//		model.addAttribute("cars", cars);
		
//		List<String> categories = carService.getAvalibleBrands();
		
//		model.addAttribute("categories", categories);
		model.addAttribute("showCategories", true);
		
		return "offerView";
	}

	@RequestMapping(value = "reload.do", params = "page")
	public @ResponseBody
	String reload(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model, @RequestParam Long page) {
//		int all = carService.getAvailableCars().size();
//		int pages = (int) Math.ceil(((double) all / (double) PAGINATE_NO));
//		if (page < 0 || page == null || page > pages) {
//			page = new Long(1);
//		}
//		List<Car> list = carService.getAvalibleCarsByPage(page.intValue());
//		ArrayList<Car> cars = new ArrayList<Car>(list);

//		List<Integer> p = getPages(pages, page.intValue());

//		return createData(new Long(pages), page, p, cars);
		return null;
	}

	public List<Integer> getPages(int pagesMax, int choosen) {
		int last = 0;
		List<Integer> pages = new ArrayList<Integer>();
		if (pagesMax > 7) {
			if (choosen > 3) {
				last = choosen + 4;
				if (last < pagesMax) {
					pages.add(choosen - 2);
					pages.add(choosen - 1);
					pages.add(choosen);
					pages.add(choosen + 1);
					pages.add(choosen + 2);
					pages.add(choosen + 3);
					pages.add(choosen + 4);
				} else {
					pages.add(pagesMax - 6);
					pages.add(pagesMax - 5);
					pages.add(pagesMax - 4);
					pages.add(pagesMax - 3);
					pages.add(pagesMax - 2);
					pages.add(pagesMax - 1);
					pages.add(pagesMax);
				}
			} else {
				for (int i = 0; i < 7; i++) {
					pages.add(i + 1);
				}
			}
		} else {
			for (int i = 0; i < pagesMax; i++) {
				pages.add(i + 1);
			}
		}

		return pages;
	}

//	private String createData(Long last, Long actual, List<Integer> pages,
//			List<Car> cars) {
//		String data = "<div id=\"cars-div\">";
//		data += "<div id=\"listasamochodow1\">"
//				+ "<p><h2>Dostepne samochody do wypozyczenia</h2></p>";
//		for (Car car : cars) {
//			data += "<div>";
//			for (Photo photo : car.getPhotos()) {
//				data += "<img src=\"../static/images/cars/" + photo.getPhoto()
//						+ ".jpg\" alt=\"Brak zdjecia\" />";
//			}
//			data += "<table>" + "<tr>" + "<th>Producent</td><td>"
//					+ car.getBrand()
//					+ "</td>"
//					+ "<th>Model</td><td>"
//					+ car.getModel()
//					+ "</td>"
//
//					+ "</tr>"
//					+ "<tr>"
//					+ "<th>Rok produkcji</td><td>"
//					+ car.getYear()
//					+ "</td>"
//					+ "<th>Numer rejestracyjny</td><td>"
//					+ car.getRegistration_number()
//					+ "</td>"
//					+ "</tr>"
//					+ "<tr>"
//					+ "<th>Kolor</td><td>"
//					+ car.getColor()
//					+ "</td>"
//					+ "<th>Opis</td><td>"
//					+ car.getDescription()
//					+ "</td>"
//					+ "</tr>"
//					+ "</table>"
//					+ "<a id=\"noDecoration\" href=\"/ssi/rent/confirm.do?id="+car.getId()+ "\">"
//					+ "<input id=\"rentButton\" type=\"button\" value=\"Wypozycz\"></input></a>"
//					+ "<br /><br />"
//
//					+ "</div>";
//		}

//		if (actual.intValue() > 1) {
//			data += "<a id=\"noDecoration\" href=\"#\" onclick=\"ajaxFunction("
//					+ (actual.intValue() - 1) + ");return false;\"><input type=\"button\" value=\"Wstecz\"></input></a>";
//		}
//
//		for (Integer p : pages) {
//
//			if (p.intValue()==actual.intValue()) {
//				data += "<a id=\"noDecoration\" href=\"#\" onclick=\"ajaxFunction("
//						+ p.intValue()
//						+ ");return false;\">"
//						+ "<input id=\"actualPageNumber\" type=\"button\" value=\"" +  p.intValue() + "\"></input></a>";
//			} else {
//				data += "<a id=\"noDecoration\" href=\"#\" onclick=\"ajaxFunction(" + p.intValue()
//						+ ");return false;\">" + "<input id=\"pageNumber\" type=\"button\" value=\"" +  p.intValue() + "\"></input></a>";
//			}
//
//		}
//		if (last > actual) {
//			data += "<a id=\"noDecoration\" href=\"#\" onclick=\"ajaxFunction("
//					+ (actual.intValue() + 1) + ");return false;\"><input type=\"button\" value=\"Dalej\"></input></a>";
//		}
//		data +="<div id=\"main_content\"></div>";
//		data +="</div>";
//		return data;
//	}
	
	@RequestMapping(value = "image.do", method = RequestMethod.GET)
	public void image(HttpSession session, Model model, Long id,
			HttpServletResponse response) throws IOException {

		System.out.println(id+"sdjfshfjshkjfshlkjdfhjkaewhsfkjsdnhklfjwh");
		
		response.setContentType("image/jpeg");
		OutputStream out = response.getOutputStream();
		Photo foto=photoService.getPhotoById(id.intValue());
		out.write(foto.getPhoto());
		out.close();

//		return "user/gallery";
	}
	
}
